package com.qdone.module.controller;

import com.alibaba.fastjson.JSON;
import com.qdone.common.util.SerialNo;
import com.qdone.framework.annotation.Function;
import com.qdone.framework.core.BaseController;
import com.qdone.module.model.Solr;
import com.qdone.module.model.TableData;
import com.qdone.module.service.SysCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *代码生成
 * @付为地
 * @date 2018-05-15 03:16:12
 */
@Controller
@RequestMapping("/sysCode")
@Api(tags = "代码生成",description = "代码生成管理")
public class SysCodeController extends BaseController{
  

    @Autowired
	private SysCodeService sysCodeService;
    
    
    /**
	 * 页面初始化
	 */
	@ApiOperation(value = "代码生成列表",notes = "进入列表页", httpMethod = "GET")
	@RequestMapping(value = "init",method = RequestMethod.GET)
	public String init(){
		return "sysCode/selectSysCode";
	}
	
    /**
	 * 分页查询数据
	 */
	/*@RequestMapping(value="/selectPage",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "分页列表", notes = "分页列表", httpMethod = "POST",response = Map.class)
	public Map<String, Object> selectPage(@RequestBody TableData entity){
		return responseSelectPage(sysCodeService.selectPage(entity));
	}*/
	@RequestMapping(value="/selectPage",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "分页列表", notes = "分页列表", httpMethod = "POST",response = Map.class)
	public Map<String, Object> selectPage(@RequestBody Map<String, Object> params){
		return responseSelectPage(sysCodeService.queryPage(params));
	}





	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String[] tableNames = new String[]{};
		String tables = request.getParameter("tables");
		tableNames =tables.split(",") ;

		byte[] data = sysCodeService.generatorCode(tableNames);

		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"AutoCode.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}
	
}
